<template>
  <div class="app-container">

    <el-table v-loading="loading" :data="exampleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="实验环境名称" align="center" prop="environment.teachingEnvironmentName" />
      <el-table-column label="实验时间" align="center" prop="environment.teachingEnvironmentTime">
        <template slot-scope="scope">
            {{ scope.row.environment.teachingEnvironmentTime }}小时
        </template>
      </el-table-column>
      <el-table-column label="实例生成时间" align="center" prop="exampleCreateTime" />
      <el-table-column label="实例结束时间" align="center" prop="exampleEndTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教学实验环境实例对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <div style="width: 100%;">
            <span style="color: #CC6600;">得分：0分 剩余时间：{{ remainingTime.hours }}时{{ remainingTime.minutes }}分{{ remainingTime.seconds }}秒</span>
        <span style="float: right;margin-right: 65px;">
            <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="resetExperiment()"
          >重做实验</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="endExperiment()"
          >结束实验</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="addExperimentTime()"
          >增加实验时间</el-button>
        </span>
        </div>
        <div>
            <el-image :src="require('@/assets/images/1.png')" fit="cover"></el-image>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listExample, getExample, delExample, addExample, updateExample,resetExperiment,endExperiment,addExperimentTime } from "@/api/experiment/example";

export default {
  name: "Example",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教学实验环境实例表格数据
      exampleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
        targetTime: null,
        currentTime: null, // 当前时间
    };
  },
  created() {
      this.getList();
        // 每秒钟更新当前时间
        setInterval(() => {
      this.currentTime = new Date();
    }, 1000);
    },
    computed: {
        remainingTime() {
      let timeDiff = this.targetTime - this.currentTime;
      let hours =0;
      let minutes = 0;
      let seconds =0;
        if (timeDiff > 0) {
              // 获取剩余时间的小时、分钟、秒数
       hours = Math.floor(timeDiff / (1000 * 60 * 60));
       minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60));
       seconds = Math.floor((timeDiff % (1000 * 60)) / 1000);
            } 
      
      return {
        hours,
        minutes,
        seconds
      };
    },
  },
  methods: {
    /** 查询教学实验环境实例列表 */
    getList() {
      this.loading = true;
      listExample(this.queryParams).then(response => {
        this.exampleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        teachingEnvironmentExampleId: null,
        teachingEnvironmentId: null,
        environmentName: null,
        testTime: null,
        exampleCreateTime: null,
        exampleEndTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.teachingEnvironmentExampleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教学实验环境实例";
    },
    /** 查看按钮操作 */
    handleUpdate(row) {
      this.reset();
      const teachingEnvironmentExampleId = row.teachingEnvironmentExampleId || this.ids
      getExample(teachingEnvironmentExampleId).then(response => {
          this.form = response.data;
          this.targetTime = new Date(this.form.exampleEndTime);
        this.open = true;
        this.title = "教学实验环境实例";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.teachingEnvironmentExampleId != null) {
            updateExample(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExample(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const teachingEnvironmentExampleIds = row.teachingEnvironmentExampleId || this.ids;
      this.$modal.confirm('是否确认删除教学实验环境实例编号为"' + teachingEnvironmentExampleIds + '"的数据项？').then(function() {
        return delExample(teachingEnvironmentExampleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('experiment/example/export', {
        ...this.queryParams
      }, `example_${new Date().getTime()}.xlsx`)
      },
      //重做实验
      resetExperiment() {
        var examId = this.form.teachingEnvironmentExampleId
          resetExperiment(examId).then(response=> {
              if (response.code == 200) {
                this.$modal.msgSuccess(response.msg);
                getExample(examId).then(response => {
          this.form = response.data;
                    this.targetTime = new Date(this.form.exampleEndTime);
                    this.getList();
      });
            }
        })
      },
      //结束实验
      endExperiment() {
        var now=new Date()
          if (this.targetTime > now) {
            var examId = this.form.teachingEnvironmentExampleId
        endExperiment(examId).then(response=> {
              if (response.code == 200) {
                this.$modal.msgSuccess(response.msg);
                getExample(examId).then(response => {
          this.form = response.data;
                    this.targetTime = new Date(this.form.exampleEndTime);
                    this.getList();
      });
            }
        })
          } else {
            this.$modal.msgWarning("实验已经结束");
        }
      },
      //增加实验时间
      addExperimentTime() {
          var environment = this.form.environment;
          if (environment.isAdd == '是' && environment.addCount > 0) {
            var examId = this.form.teachingEnvironmentExampleId
            addExperimentTime(examId).then(response=> {
              if (response.code == 200) {
                this.$modal.msgSuccess(response.msg);
                getExample(examId).then(response => {
          this.form = response.data;
                    this.targetTime = new Date(this.form.exampleEndTime);
                    this.getList();
      });
            }
        })
          } else {
            this.$modal.msgError("不能增加实验时间！");
          }
      }
  }
};
</script>
